<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form
          v-show="showSearch"
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="模板名称" prop="userName">
            <el-input
              v-model="name"
              placeholder="请输入板块名称"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete"
              :disabled="multiple"
              >删除</el-button
            >
          </el-col>

          <!-- <right-toolbar
            :show-search.sync="showSearch"
            :columns="columns"
            @queryTable="getList"
          /> -->
        </el-row>

        <el-table
          :data="CategoryList"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection"  />
          <el-table-column
            prop="id"
            label="板块编号"
            align="center"

          />
          <el-table-column
            prop="name"
            label="板块名称"
            align="center"

          />
          <el-table-column
            prop="description"
            label="板块描述"
            align="center"

          />
          <el-table-column prop="adminName" label="版主" align="center"  >
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.adminName"
                @change="((value)=>{handlesetAdmin(value, scope.row.id)})"
              >
                <el-option
                  v-for="item in admin"
                  :label="item.nickname"
                  :key="item.id"
                  :value="item.id"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="350"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="Delete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <el-pagination
      :page-size.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page.sync="queryParams.pageNum"
      @current-change="handleCurrentChange"
      @size-change="handleCurrentChange"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="板块名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入板块名称"
                maxlength="15"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="板块描述" prop="description">
              <el-input
                v-model="form.description"
                placeholder="请输入板块描述"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addCategory,
  delCategory,
  getCate,
  getCategoryById,
  updateCategory,
  searchCategory,
} from "@/api/content/category";
import {
 categoryAdmin,setCategoryAdmin
} from "@/api/system/admin";
import {
  treeselect as menuTreeselect,
  roleMenuTreeselect,
} from "@/api/system/menu";

export default {
  name: "User",
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      name: "",
      CategoryList: [],
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单校验
      rules: {
        name: [
          { required: true, message: "板块名称不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "板块描述不能为空", trigger: "blur" },
          {
            min: 5,
            max: 20,
            message: "板块描述必须介于 5 和 20 之间",
            trigger: "blur",
          },
        ],
      },
      // 角色选项
      roleOptions: [],
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      // 用户表格数据
      userList: null,
      // 总条数
      total: 0,
      // 选中数组
      ids: [],
      // 表单参数
      form: {},
      type: "",
      multiple: true,
      admin:[],
      adminId:""
    };
  },
  watch: {},
  created() {
    this.search(this.name, 1, this.queryParams.pageSize);
    this.categoryAdmin();
  },
  methods: {
    handlesetAdmin(value,id){
      setCategoryAdmin(id,value).then((response) => {

        this.categoryAdmin();
      });


    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.loading = true;
      this.search(this.name, 1, this.queryParams.pageSize);
    },
    search(name, page, size) {
      this.loading = true;
      searchCategory(name, page, size).then((response) => {
        this.CategoryList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(this.CategoryList);
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 查询用户列表 */

    getCategory(page, size) {
      this.loading = true;
      getCate(page, size).then((response) => {
        this.CategoryList = response.rows;
        this.total = response.total;
        console.log(this.CategoryList);
        this.loading = false;
      });
    },
    handleCurrentChange() {
      this.search(
        this.name,
        this.queryParams.pageNum,
        this.queryParams.pageSize
      );
      console.log(this.queryParams.pageNum);
    },
    categoryAdmin()
    {
        categoryAdmin().then((response) => {
        this.admin = response;
        console.log(response);
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.type = "2";
      this.open = true;
      console.log(row.id);
      getCategoryById(row.id).then((response) => {
        this.form.name = response.name;
        this.form.description = response.description;
        this.form.id = response.id;
        console.log(response);
      });
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        description: undefined,
        id: undefined,
      };
      this.resetForm("form");
    },
    /** 新增用户 */
    handleAdd() {
      this.reset();
      this.type = "1";
      this.open = true;
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = this.ids;
      this.$modal
        .confirm('是否确认删除板块编号为"' + ids + '"的数据项？')
        .then(function () {
          var i = 0;
          while (ids[i + 1]) {
            delCategory(ids[i]);
            i = i + 1;
          }
          return delCategory(ids[i]);
        })
        .then(() => {
          this.queryParams.pageNum = 1;
          this.search(this.name, 1, this.queryParams.pageSize);
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    Delete(row) {
      this.$modal
        .confirm('是否确认删除板块编号为"' + row.id + '"的数据项？')
        .then(function () {
          return delCategory(row.id);
        })
        .then(() => {
          this.queryParams.pageNum = 1;
          this.search(this.name, 1, this.queryParams.pageSize);
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 提交按钮 */
     /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate((valid) => {
        if(valid)
        {
      this.open = false;
      if (this.type == "1") {
        addCategory(this.form.name, this.form.description).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.queryParams.pageNum = 1;
          this.search(this.name, 1, this.queryParams.pageSize);
        });
      }
      if (this.type == "2") {
        console.log(this.form.description);
        updateCategory(
          this.form.id,
          this.form.name,
          this.form.description
        ).then((response) => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.queryParams.pageNum = 1;
          this.search(this.name, 1, this.queryParams.pageSize);
        });
      }}
    });
    },
  },
};
</script>
